<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Equipment</title>
	<link rel="stylesheet" href="../js/easyui/themes/bootstrap/easyui.css">
	<link rel="stylesheet" href="../js/easyui/themes/icon.css">
	<link rel="stylesheet" href="../js/easyui/themes/color.css">
	<link rel="stylesheet" href="../css/index-ui.css" />
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/ams.util.js"></script>
	<script type="text/javascript" src="../js/webapi.js"></script>
	<script type="text/javascript" src="../js/ui_eq_group.js"></script>
	<script type="text/javascript" src="../js2/loginCookies.js"></script>
</head>
<body onload="on_page_load()">
	<canvas id="canvas_main" left="50" width="1000" height="900" background-color="#333333" style="border:0px solid #c3c3c3;">
		Your browser does not support the canvas element.
	</canvas>

	<div id="unit_window" class="easyui-window" title="Unit Detail"
		data-options="modal:true, closed:true,iconCls:'icon-save',cls:'c1'" style="width:500px;height:400px;padding:10px;">
	
		<label id="unit_name_label">{uname}</label>
		<div style="width:99%; margin-right:10px">
			<table style="color:#777777;border:solid thin #cccccc">
				<tr>
					<td width="300">
						<table style="padding-left:10px" id="table_left">
						</table>
					</td>
					<td width="300">
						<table style="padding-left:10px" id="table_right">
						</table>
					</td>
				</tr>
			</table>
		</div>
		</br>		
		
		<table id="data_table" class="easyui-datagrid" style="width:99%;height:70%" iconCls="icon-save"  rownumbers="false" pagination="false">
			<thead>
				<tr>
					<th data-options="field:'name',width:165">Name</th>
					<th data-options="field:'value',width:165">Value</th>
				</tr>
			</thead>
		</table>
	</div>

</body>
<script type="text/javascript">
var g_json_data_units = null
var eqn = parent.$("#eq_name").val()
var eqt = '';
function __set_mouse_event()
{
	canvas_main.onmousemove = function(e)
	{
		var location = getLocation(this, e.clientX, e.clientY)
		
		for (var i = 0; i < g_json_data_units.eq_unit_array.length; i++) {
			var unit = g_json_data_units.eq_unit_array[i];
			if (location.x >= unit.x && location.x <= (unit.x + unit.width) &&
				location.y >= unit.y && location.y <= (unit.y + unit.height))
			{
				if (__is_pm(unit.type) || __is_lp(unit.type, unit.carrier)) {
					this.style.cursor = 'pointer'
					return
				}
			}
		}
		this.style.cursor = 'auto'
	}

	canvas_main.onmousedown = function(e)
	{
		var location = getLocation(this, e.clientX, e.clientY);
		
		for (var i = 0; i < g_json_data_units.eq_unit_array.length; i++) {
			var unit = g_json_data_units.eq_unit_array[i];
			if (location.x >= unit.x && location.x <= (unit.x + unit.width) &&
				location.y >= unit.y && location.y <= (unit.y + unit.height))
			{
				if (__is_pm(unit.type)) {
					// show unit detail window
					show_unit_detail_window(unit.name)
					return
				}
				else if (__is_lp(unit.type, unit.carrier)) {
					show_sub_cassette(unit.name)
					return
				}
			}
		}
	}
}

function on_page_load()
{
	eqt = GetCookie(eqn);
	if (ams_is_sim_mode()) {
		$.getJSON("eq_overview_01.json", function(json_data){
			g_json_data_units = json_data
			__set_mouse_event()
			update()
		})
	} else {
 		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEqUnitInfo"),
			data:JSON.stringify({
				"eqName":eqn,
				"unitName":""
			}),
			dataType: 'json',
			success: function (json_data) {
				console.log(JSON.parse(json_data.d))
				g_json_data_units = JSON.parse(json_data.d)
				__set_mouse_event()
				update()
			},
			error: function (response) {
				$.messager.alert("Error", "Error1: " + response.responseText, "error");	
				//alert('Error1: ' + response.responseText)
			}
		});
	}
}

function update_unti_table(json_data)
{
	var left = document.getElementById("table_left"); 
	var childs = left.childNodes; 
	for(var i = childs.length - 1; i >= 0; i--) { 
	  //alert(childs[i].nodeName); 
	  left.removeChild(childs[i]); 
	}

	var right = document.getElementById("table_right"); 
	childs = right.childNodes; 
	for(var i = childs.length - 1; i >= 0; i--) { 
	  //alert(childs[i].nodeName); 
	  right.removeChild(childs[i]); 
	}
	
	var nDataLength = 0;
	if (typeof(json_data.data) == "undefined")
	{
		nDataLength = Object.keys(json_data).length;
	}
	else
	{
		nDataLength = Object.keys(json_data).length - 1;
	}
	for(var i = 0; i < nDataLength; i++){
		if(i % 2 == 0){
			$("#table_left").append("<tr><td>" + Object.keys(json_data)[i] + ": " + Object.values(json_data)[i] + " </td></tr>");
		}else{
			$("#table_right").append("<tr><td>" + Object.keys(json_data)[i] + ": " + Object.values(json_data)[i] + " </td></tr>");
		}
	}
}

function update_unit_detail(unitName)
{
	if (ams_is_sim_mode()) {

		$.getJSON("eq_unit_detail_01.json", function(json_data){
				update_unti_table(json_data);
		})

		$.getJSON("eq_unit_detail_01.json", function(json_data){
			$('#data_table').datagrid({
				rownumbers:'false',
				singleSelect:'true',
				lines:true,
				clickToEdit: false,
				dblclickToEdit: false,
				loadMsg: 'Loading Data ......',
			//	columns:json_1.columns,
				data:json_data.data
			});
		})
	}else{
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEqUnitInfo"),
			data:JSON.stringify({
				"eqName": eqn,
				"unitName":unitName
			}),
			dataType: 'json',
			success: function (json_data) {
				//console.log(JSON.parse(json_data.d))
				update_unti_table(JSON.parse(json_data.d));
				if (typeof(JSON.parse(json_data.d).data) == "undefined")
				{
					$('#data_table').datagrid('loadData',{total:0,rows:[]})
				}
				else
				{
					$('#data_table').datagrid({
						rownumbers:'false',
						singleSelect:'true',
						lines:true,
						clickToEdit: false,
						dblclickToEdit: false,
						loadMsg: 'Loading Data ......',
					//	columns:json_1.columns,
						data:JSON.parse(json_data.d).data
					});
				}
			},
			error: function (response) {
				$.messager.alert("Error", "Error2: " + response.responseText, "error");	
				//alert('Error2: ' + response.responseText)
			}
		});
	}
	
}

function show_unit_detail_window(unitName)
{
	$('#unit_window').window('open')
	document.getElementById("unit_name_label").innerText = "UNIT: " + unitName

	update_unit_detail(unitName)
}

function update()
{
//	test();
	if (ams_is_sim_mode()) {
		$.getJSON("eq_overview_01.json", function(json_data){
			draw_equipment_overview("canvas_main", json_data, "no", eqt)
		})
	}
	else{
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEqUnitInfo"),
			data:JSON.stringify({
				"eqName": eqn,
				"unitName":""
			}),
			dataType: 'json',
			success: function (json_data) {
				console.log(JSON.parse(json_data.d))
				draw_equipment_overview("canvas_main", JSON.parse(json_data.d), "no", eqt)
			},
			error: function (response) {
				$.messager.alert("Error", "Error3: " + response.responseText, "error");	
			}
		});
	}
	setTimeout('update()', 1000)
}

function show_sub_cassette(name)
{
	var cm = "";
	if (name == "LP1"){
		cm = "CM1";
	}else if (name == "LP2"){
		cm = "CM2";
	}else if (name == "LP3"){
		cm = "CM3";
	}else if (name == "LP4"){
		cm = "CM4"
	}
	
	var tagNames = new Array();
	for (var i = 0; i < 25; i++){
		var name = cm + (i < 9 ? (".C0") : (".C")) + (i + 1) + "_Wafer";
		tagNames.push(name);
	}
	/*
	var tagNames = new Array();
	for (var i = 0; i < 2; i++){
		for (var j = 0; j < 10; j++){
			var name = "PLC" + (i + 1) + "_SCB_PUMP" + (j + 1) + "Name";
			tagNames.push(name);
		}
	}*/
	
	$.ajax({
	type: 'POST',
	contentType: 'application/json',
	url: getWebServiceUrl("getEquipRTDatas"),
	data:JSON.stringify({
		"eqName":eqn,
		"varNames":tagNames
	}),
	dataType: 'json',
	success: function (json_data) {
		var obj = JSON.parse(json_data.d)
		var tagValue = Number(obj.series[0].data[0].value)
		var tagTime = String(obj.series[0].data[0].time)
		//t_now = (new Date(String(obj.series[0].data[0].time)))
		//console.log(JSON.parse(json_data.d))
		if( tagValue == null)
			alert(tagValue);
	},
	error: function (response) {
		$.messager.alert("Error", "Error: " + response.responseText, "error");
	}
 });
}
</script>
</html>
